using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.Practices.Unity.Utility;
using System;

namespace Refract.Navigation
{
    /// <summary>
    /// Enumerates pages for the navigation subsystem
    /// </summary>
    public class PageItemList : INotifyCollectionChanged, IEnumerable
    {
        #region Fields

        private Dictionary<string, PageItem> _itemDictionary = new Dictionary<string, PageItem>();
        
        #endregion

        #region Properties

        /// <summary>
        /// Gets the <see cref="Refract.Navigation.PageItem"/> with the specified name.
        /// </summary>
        /// <value></value>
        public PageItem this[string name]
        {
            get { return Get(name); }            
        } 

        #endregion

        #region Public

        /// <summary>
        /// Adds the specified page.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Add(PageItem item)
        {
            Guard.ArgumentNotNullOrEmpty(item.Name, "item.Name");
            _itemDictionary.Add(item.Name, item);
            item.OnNavigate+=new EventHandler<EventArgs<string>>(Navigate);
            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, -1));
            }
        }

        /// <summary>
        /// Gets the specified page.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public PageItem Get(string name)
        {
            return _itemDictionary[name];
        }

        /// <summary>
        /// Removes the specified page.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public bool Remove(string name)
        {
            PageItem item = this[name];
            item.OnNavigate -= new EventHandler<EventArgs<string>>(Navigate);
            return _itemDictionary.Remove(name);
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator GetEnumerator()
        {
            return _itemDictionary.Values.GetEnumerator();
        }

        #endregion

        #region INotifyCollectionChanged Members

        /// <summary>
        /// Occurs when the items list of the collection has changed, or the collection is reset.
        /// </summary>
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        #endregion

        private void Navigate(object sender, EventArgs<string> e)
        {            
       
            if (OnNavigate != null)
            {
                OnNavigate(sender, e);
            }
        }

        public event EventHandler<EventArgs<string>> OnNavigate;
    }
}
